/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.cadoc.parser;

import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.LinkedList;

public class JavaStatementTokenizer implements StatementTokenizer {

	private StatementClassifier classifier;
	private static final int BUFFER_SIZE = 2048;

	public JavaStatementTokenizer() {
	}

	public void tokenize(InputStream inStream) {
		try {
			byte buf[] = new byte[BUFFER_SIZE];

			String tmpString;
			Collection<String> statement = new LinkedList<String>();
			StringBuilder statementPart = new StringBuilder();
			char tmpChar;
			boolean inlineComment = false;
			boolean blockComment = false;

			
			while (inStream.read(buf) != -1) {
				tmpString = new String(buf);
				for(int i = 0; i<tmpString.length(); i++){
					tmpChar = tmpString.charAt(i);
					
					switch(tmpChar){
					case ' ' : 
						recordPart(statementPart, statement);
						break;
					case LanguageToken.STATEMENT_CLOSER :
						recordPart(statementPart, statement);
						forward(statement);
						break;
					case LanguageToken.BLOCK_BEGIN :
						recordPart(statementPart, statement);
						forward(statement);
						break;

					case LanguageToken.COMMENT_BEGIN :
						if(tmpString.charAt((i+1))=='\n'){
							blockComment = false;
						}else if (tmpString.charAt((i+1))=='/'){
							inlineComment = true;
						}else if (tmpString.charAt((i+1))=='*'){
							blockComment = true;
						}
						break;
					
					case LanguageToken.PARAMETER_BEGIN :
						if(!blockComment && !inlineComment){
							recordPart(statementPart, statement);
							statementPart.append(tmpChar);
							recordPart(statementPart, statement);
						}
						break;
						
					case LanguageToken.PARAMETER_SEPERATOR :
						if(!blockComment && !inlineComment){
							recordPart(statementPart, statement);
							statementPart.append(tmpChar);
							recordPart(statementPart, statement);
						}
						break;
						
					case LanguageToken.ASSIGNMENT_OPERATOR :
						if(!blockComment && !inlineComment){
							recordPart(statementPart, statement);
							statementPart.append(tmpChar);
							recordPart(statementPart, statement);
						}
						break;
						
					case LanguageToken.PARAMETER_END :
						if(!blockComment && !inlineComment){
							recordPart(statementPart, statement);
							statementPart.append(tmpChar);
							recordPart(statementPart, statement);
						}
						break;
						
					case LanguageToken.BLOCK_END :
						forwardEndBlock();
						break;
						
					case '\n':
						inlineComment = false;
						break;
						
					case '\t':
						break;
						
					default :
						if(!blockComment && !inlineComment)
							statementPart.append(tmpChar);
					}
				}

				int i = 0 ;
				i++;
			}
			inStream.close();
		} catch (IOException e) {
			System.err.println(e);
		}
	}

	/**
	 * publish the String that forms a statement to all registered interpreters
	 * that observe this object.
	 * 
	 * @param statement
	 */
	private void forward(Collection<String> statement) {
		if(statement.size()>0){
			String[] stmtArr = (String[]) statement.toArray(new String[statement.size()]);
			classifier.classify(stmtArr);
			statement.clear();
		}

	}
	
	private void forwardEndBlock(){
		//interpreter.endBlockStatement();
		
	}
	
	private void recordPart(StringBuilder statementPart, Collection<String> statement){
		if(statementPart.length()>0){
			statement.add(statementPart.toString());
			statementPart.delete(0, statementPart.length());
		}
	}

	public void setStatementClassifier(StatementClassifier stmtClassifier) {
		this.classifier = stmtClassifier;
	}
}
